# -*- coding: utf-8 -*-
"""
输入适配器，将输入数据适配为计算所需的格式
"""
from utils.numeric import *
from utils.constant import *
from algorithm.source_terms.source_base_formula import *
from utils.crdlog import log

class InputAdapter:
    def __init__(self, params: dict):
        """ 初始化

        Args:
            params (dict): 输入参数
            physical_property_calculator (): 物性计算器
        """
        self.params = params
        self.standard_input = {}

    def _interp_node_to_segment(self, var_name: str):
        """ 将节点数据转换为段数据
        
        """
        var_node = self.params[var_name]
        var_segment = grid_interp(var_node, InterpMode.MID)
        return var_segment

    def _convert_origin_var_to_standard_var(self):
        """ 将原始变量转换为标准变量
        
        """
        var_list_origin = ['dx',
                    'V_L_segment', 
                    'V_g_segment',
                    'V_D_segment',
                    'alpha_L_node',
                    'alpha_g_node',
                    'alpha_D_node',
                    'rho_L_node',
                    'rho_g_node',
                    'p_node',
                    'T_node',
                    'tilt_angle_segment',
                    'D_segment',
                    'cross_section_area_segment',
                    'wall_roughness_segment']
        
        var_list_standard = ['dx',
                    'V_L_segment', 
                    'V_g_segment',
                    'V_D_segment',
                    'alpha_L_segment',
                    'alpha_g_segment',
                    'alpha_D_segment',
                    'rho_L_segment',
                    'rho_g_segment',
                    'p_segment',
                    'T_segment',
                    'tilt_angle_segment',
                    'D_segment',
                    'cross_section_area_segment',
                    'wall_roughness_segment']
        
        for i in range(len(var_list_origin)):
            if 'node' in var_list_origin[i]:
                self.standard_input[var_list_standard[i]] = self._interp_node_to_segment(var_list_origin[i])
            else:
                self.standard_input[var_list_standard[i]] = self.params[var_list_origin[i]]

    def _gen_physical_properties(self):
        """ 生成物性参数
        
        """
        self.standard_input['liquid_surface_tension_segment'] = self._interp_node_to_segment('liquid_surface_tension_node')
        self.standard_input['liquid_viscosity_segment'] = self._interp_node_to_segment('liquid_viscosity_node')
        self.standard_input['gas_viscosity_segment'] = self._interp_node_to_segment('gas_viscosity_node')
        self.standard_input["gas_mass_fraction_segment"] = self._interp_node_to_segment('gas_mass_fraction_node')
        
        
    def _gen_derived_var(self):
        """ 生成衍生变量; TODO: 后续调整只需修改衍生变量
        
        """
        self.standard_input['V_sg_segment'] = cal_superficial_gas_velocity(self.standard_input['V_g_segment'], self.standard_input['alpha_g_segment'])
        self.standard_input['V_sL_segment'] = cal_superficial_liquid_velocity(self.standard_input['V_L_segment'], self.standard_input['alpha_L_segment'])
        self.standard_input['V_r_segment'] = cal_relative_velocity_between_phases(self.standard_input['V_g_segment'], self.standard_input['V_L_segment'])
    
    def get_standard_input(self):
        """ 将输入数据转换为计算所需的格式
        
        """
        self._convert_origin_var_to_standard_var()
        self._gen_physical_properties()
        self._gen_derived_var()
        return self.standard_input